#!/bin/bash
# chkconfig: 2345 90 10
# description: A secure socks5 proxy, designed to protect your Internet traffic.

### BEGIN INIT INFO
# Provides:          Shadowsocks-libev
# Required-Start:    $network $syslog
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Fast tunnel proxy that helps you bypass firewalls
# Description:       Start or stop the Shadowsocks-libev server
### END INIT INFO

# Intro:  https://github.com/onekeyshell/kcptun_for_ss_ssr/issues

# Source function library
if [ -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
# Check that networking is up.
    [ ${NETWORKING} ="yes" ] || exit 0
fi

if [ -f /usr/local/bin/ss-server ]; then
    DAEMON=/usr/local/bin/ss-server
elif [ -f /usr/bin/ss-server ]; then
    DAEMON=/usr/bin/ss-server
fi
NAME=Shadowsocks-libev
CONF=/etc/shadowsocks-libev/config.json
PID_DIR=/var/run
PID_FILE=$PID_DIR/shadowsocks-libev.pid
version="2.0"
RET_VAL=0

[ -x $DAEMON ] || exit 0

if [ ! -f $CONF ]; then
    echo "$NAME config file $CONF not found"
    exit 1
fi

if [ ! -d $PID_DIR ]; then
    mkdir -p $PID_DIR
    if [ $? -ne 0 ]; then
        echo "Creating PID directory $PID_DIR failed"
        exit 1
    fi
fi

check_running() {
    if [ -r $PID_FILE ]; then
        read PID < $PID_FILE
        if [ -d "/proc/$PID" ]; then
            return 0
        else
            rm -f $PID_FILE
            return 1
        fi
    else
        return 2
    fi
}

do_start() {
    if [ -f $PID_FILE ]; then
        read PID < $PID_FILE
        echo "$NAME (pid $PID) is already running..."
        exit 0
    else
        echo -n $"Starting ${NAME}: "
        if [ -f /etc/rc.d/init.d/functions ]; then
            daemon $DAEMON -c $CONF -u -f $PID_FILE
        else
            $DAEMON -c $CONF -u -f $PID_FILE
        fi
    fi
    RETVAL=$?
    if [ -f /etc/rc.d/init.d/functions ]; then
        [ $RETVAL -eq 0 ] && success
    else
        if check_running; then
            echo "    [ok]"
        else
            echo "    [FAILED]"
            RETVAL=1
        fi
    fi
}

do_stop() {
    echo -n $"Stopping ${NAME}: "
    if [ -f /etc/rc.d/init.d/functions ]; then
        killproc -p $PID_FILE
    else
        if check_running; then
            kill -9 $PID
            echo "    [ok]"
        else
            echo "    [FAILED]"
            RETVAL=1
        fi
    fi
    RETVAL=$?
    [ $RETVAL -eq 0 ]
    rm -f $PID_FILE
}

do_status() {
    check_running
    case $? in
        0)
        echo "$NAME (pid $PID) is running..."
        ;;
        1|2)
        echo "$NAME is stopped"
        RET_VAL=1
        ;;
    esac
}

do_restart() {
    do_stop
    do_start
}

do_config() {
    vi $CONF
}

do_version(){
    ${DAEMON} -h | grep -i "shadowsocks-libev"
}

case "$1" in
    start|stop|restart|status|config)
    do_$1
    ;;
    [vV][eE][rR][sS][iI][oO][nN]|-[vV][eE][rR][sS][iI][oO][nN]|--[vV][eE][rR][sS][iI][oO][nN]|-[vV]|--[vV])
        do_version
    ;;
    *)
    echo "Usage: $0 { start | stop | restart | status | config | version }"
    RET_VAL=1
    ;;
esac

exit $RETVAL
